package su.mvc.action.admin;

import com.googlecode.objectify.Key;
import org.apache.struts2.convention.annotation.*;
import su.mvc.action.ActionSupport;
import su.mvc.service.UserService;
import su.mvc.service.gui.ImageService;
import su.mvc.service.gui.MenuService;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

@Results(value = {
        @Result(name = ActionSupport.SUCCESS, location = "upload-form.jsp"),
        @Result(name = ActionSupport.REDIRECT, type = "redirect", location = "/admin/upload", params = {"messages", "image.upload.successfully"}),
        @Result(name = ActionSupport.ACCESS_DENIED, location = "access-denied.jsp")
})
@ParentPackage("default")
public class UploadAction extends ActionSupport {

    private static final Logger log = Logger.getLogger(UploadAction.class.getName());
    
    private ImageService imageService;
    
    private MenuService menuService;
    
    private UserService userService;
    
    private Key parentItemKey;


    private List<String> uploads = new ArrayList<String>();
    private List<String> uploadFileNames = new ArrayList<String>();
    private List<String> uploadContentTypes = new ArrayList<String>();
    private List<InputStream> uploadStreams = new ArrayList<InputStream>();

    private Map<String, String> menuItems;

    public ImageService getImageService() {
        return imageService;
    }

    public void setImageService(ImageService imageService) {
        this.imageService = imageService;
    }

    public List<InputStream> getUploadStreams() {
        return uploadStreams;
    }

    public void setUploadStreams(List<InputStream> uploadStreams) {
        this.uploadStreams = uploadStreams;
    }

    public List<String> getUpload() {
        return uploads;
    }

    public void setUpload(List<String> uploads) {
        this.uploads = uploads;
    }

    public List<String> getUploadFileName() {
        return this.uploadFileNames;
    }
    public void setUploadFileName(List<String> uploadFileNames) {
        this.uploadFileNames = uploadFileNames;
    }

    public List<String> getUploadContentType() {
        return this.uploadContentTypes;
    }
    public void setUploadContentType(List<String> contentTypes) {
        this.uploadContentTypes = contentTypes;
    }
    
    public String execute() throws Exception{
        if (!getUserService().isUserAdmin()) {
            return ACCESS_DENIED;
        }
        menuItems = menuService.getMenuItemsMap("-");
        return SUCCESS;
    }

    @Action(interceptorRefs=@InterceptorRef("gaeFileUploadStack"))
    public String upload() throws Exception {
        if (!getUserService().isUserAdmin()) {
            return ACCESS_DENIED;
        }
        imageService.saveImages(getUpload(), getUploadFileName(), getUploadContentType(), getParentItemKey());
        return ActionSupport.REDIRECT;
    }

    public Map<String, String> getMenuItems() {
        return menuItems;
    }

    public void setMenuItems(Map<String, String> menuItems) {
        this.menuItems = menuItems;
    }

    public MenuService getMenuService() {
        return menuService;
    }

    public void setMenuService(MenuService menuService) {
        this.menuService = menuService;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public Key getParentItemKey() {
        return parentItemKey;
    }

    public void setParentItemKey(Key parentItemKey) {
        this.parentItemKey = parentItemKey;
    }
}
